package cn.com.cpic.insurance.rulecheck.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.cpic.insurance.rulecheck.entity.ConditionItem;

@Service
@Transactional
public class ConditionItemService {

	private JdbcTemplate jdbcTemplate;

	@Transactional(readOnly = true)
	public List<ConditionItem> getConditionItemList(final Long conditionId) {
		return jdbcTemplate.query("SELECT * FROM t_rc_condition_item WHERE c_condition_id=? ORDER BY c_id",
				new Object[] { conditionId },
				new RowMapper<ConditionItem>() {
					public ConditionItem mapRow(ResultSet rs, int index)
							throws SQLException {
						ConditionItem item = new ConditionItem();
						item.setId(rs.getLong("c_id"));
						item.setConditionId(conditionId);
						item.setParamName(rs.getString("c_param_name"));
						item.setParamOperator(rs.getInt("c_param_operator"));
						item.setParamValue(rs.getString("c_param_value"));
						item.setRemark(rs.getString("c_rem"));
						return item;
					}
				});
	}

	@Transactional(readOnly = true)
	public ConditionItem getConditionItem(final Long id) {
		return jdbcTemplate.query("SELECT * FROM t_rc_condition_item WHERE c_id=?",
				new Object[] { id }, new ResultSetExtractor<ConditionItem>() {
					public ConditionItem extractData(ResultSet rs)
							throws SQLException, DataAccessException {
						rs.next();
						ConditionItem item = new ConditionItem();
						item.setId(id);
						item.setConditionId(rs.getLong("c_condition_id"));
						item.setParamName(rs.getString("c_param_name"));
						item.setParamOperator(rs.getInt("c_param_operator"));
						item.setParamValue(rs.getString("c_param_value"));
						item.setRemark(rs.getString("c_rem"));
						return item;
					}
				});
	}

	public long saveConditionItem(final ConditionItem item) {
		if (item.getId() == null) {
			jdbcTemplate.update(
					"INSERT INTO t_rc_condition_item VALUES(nextval('seq_id'),?,?,?,?,?)",
					new PreparedStatementSetter() {
						public void setValues(PreparedStatement ps)
								throws SQLException {
							ps.setLong(1, item.getConditionId());
							ps.setString(2, item.getParamName());
							ps.setInt(3, item.getParamOperator());
							ps.setString(4, item.getParamValue());
							ps.setString(5, item.getRemark());
						}
					});
			return jdbcTemplate.queryForLong("SELECT currval('seq_id')");
		} else {
			jdbcTemplate
					.update("UPDATE t_rc_condition_item SET c_condition_id=?,c_param_name=?,c_param_operator=?,c_param_value=?,c_rem=? WHERE c_id=?",
							new PreparedStatementSetter() {
								public void setValues(PreparedStatement ps)
										throws SQLException {
									ps.setLong(1, item.getConditionId());
									ps.setString(2, item.getParamName());
									ps.setInt(3, item.getParamOperator());
									ps.setString(4, item.getParamValue());
									ps.setString(5, item.getRemark());
									ps.setLong(6, item.getId());
								}
							});
			return item.getId();
		}
	}

	public void deleteConditionItem(final Long id) {
		jdbcTemplate.update("DELETE FROM t_rc_condition_item WHERE c_id=?", id);
	}

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
}
